package com.data_structure_algorithm.data_structure.stack;

/**
 * @author 何章怀晓 hezhanghuaixiao
 * @version V1.0
 * @desc  栈
 */
public class StackArray {

  int[] stack;

  int max;

  public int top = -1;

  public StackArray(int max) {
    this.max = max;
    stack = new int[max];
  }

  //add

  public void push(int num) {
    if (top == max - 1) {
      System.out.println("已满");
      return;
    }
    //添加
    stack[++top] = num;


  }

  public int pop() {
    if (top == -1) {
      throw new RuntimeException("栈内无元素");
    }

    return stack[top--];
  }

  public int peek(){
    if (top == -1) {
      throw new RuntimeException("栈内无元素");
    }

    return stack[top];
  }


  public void showStack(int[] stackArr) {

    //
    for (int i = top; i >= 0; i--) {
      System.out.println(stackArr[i]);
    }
  }


  //判断优先级
  //数字代表优先级
  public int priority(int oper) {
    if (oper == '+' || oper == '-') {
      return 0;
    }
    if (oper == '*' || oper == '/') {
      return 1;
    }

    return -1;
  }


  //判断当前符号是否为运算符
  public boolean isOper(int oper) {
    return oper == '+' || oper == '-' || oper == '*' || oper == '/';
  }


  //约定
  //     - / 后弹出的数对先弹出的数操作
  public int cal(int v1, int v2, int oper) {
    //oper为操作符
    switch (oper) {
      case '+':
        return v1 + v2;
      case '-':
        return v2 - v1;
      case '*':
        return v1 * v2;
      case '/':
        return v2 / v1;
    }
    return -1;
  }

}
